package com.sike.netty.jinjie.bytebuf.api;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.ByteBufUtil;

public class SliceTest {
    public static void main(String[] args) {
        ByteBuf originalByteBuf = ByteBufAllocator.DEFAULT.buffer(12,24);

        // 写入 9 个字符
        originalByteBuf.writeBytes(new byte[]{'a','b','c','d','e','f','g','h','i'});
        // 读取 4 个字符
        originalByteBuf.readInt();
        ByteBufPrintUtil.printByteBuf(originalByteBuf,"originalByteBuf");

        //产生一个切片
        ByteBuf sliceByteBuf1 = originalByteBuf.slice();
        ByteBufPrintUtil.printByteBuf(sliceByteBuf1,"sliceByteBuf1");

        // 产生一个切片
        ByteBuf sliceByteBuf2 = originalByteBuf.slice(4,8);
        ByteBufPrintUtil.printByteBuf(sliceByteBuf2,"sliceByteBuf2");

        // 改变前
        System.out.println("sliceByteBuf1 :" + sliceByteBuf1.getByte(2));
        System.out.println("sliceByteBuf2 :" + sliceByteBuf2.getByte(2));
        originalByteBuf.setByte(6,9);
        // 改变后
        System.out.println("sliceByteBuf1 :" + sliceByteBuf1.getByte(2));
        System.out.println("sliceByteBuf2 :" + sliceByteBuf2.getByte(2));

        // 写入数据
        originalByteBuf.writeBytes(new byte[]{'j','k','l','m','n'});
        StringBuilder builder = ByteBufPrintUtil.getPrintBuilder(originalByteBuf,"originalByteBuf");
        ByteBufUtil.appendPrettyHexDump(builder,originalByteBuf);
        System.out.println(builder.toString());

        builder =  ByteBufPrintUtil.getPrintBuilder(sliceByteBuf1,"sliceByteBuf1");
        ByteBufUtil.appendPrettyHexDump(builder,sliceByteBuf1);
        System.out.println(builder.toString());

        builder =  ByteBufPrintUtil.getPrintBuilder(sliceByteBuf2,"sliceByteBuf2");
        ByteBufUtil.appendPrettyHexDump(builder,sliceByteBuf2);
        System.out.println(builder.toString());

    }
}
